home *** CD-ROM | disk | FTP | other *** search
/ Openstep 4.2 (Developer) / Openstep Developer 4.2.iso / NextDeveloper / Source / GNU / uucp / Uucp.framework / unix.subproj / status.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-09  |  5.6 KB  |  249 lines

  1. /* status.c
  2.    Routines to get and set the status for a system.
  3.  
  4.    Copyright (C) 1991, 1992, 1993, 1995 Ian Lance Taylor
  5.  
  6.    This file is part of the Taylor UUCP package.
  7.  
  8.    This program is free software; you can redistribute it and/or
  9.    modify it under the terms of the GNU General Public License as
  10.    published by the Free Software Foundation; either version 2 of the
  11.    License, or (at your option) any later version.
  12.  
  13.    This program is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    You should have received a copy of the GNU General Public License
  19.    along with this program; if not, write to the Free Software
  20.    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    The author of the program may be contacted at ian@airs.com or
  23.    c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
  24.    */
  25.  
  26. #include "uucp.h"
  27.  
  28. #include "uudefs.h"
  29. #include "uuconf.h"
  30. #include "sysdep.h"
  31. #include "system.h"
  32.  
  33. #include <errno.h>
  34. #include <ctype.h>
  35.  
  36. #if SPOOLDIR_HDB || SPOOLDIR_SVR4
  37.  
  38. /* If we are using HDB spool layout, store status using HDB status
  39.    values.  SVR4 is a variant of HDB.  */
  40.  
  41. #define MAP_STATUS 1
  42.  
  43. static const int aiMapstatus[] =
  44. {
  45.   0, 13, 7, 6, 20, 4, 3, 2
  46. };
  47. #define CMAPENTRIES (sizeof (aiMapstatus) / sizeof (aiMapstatus[0]))
  48.  
  49. #else /* ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 */
  50.  
  51. #define MAP_STATUS 0
  52.  
  53. #endif /* ! SPOOLDIR_HDB && ! SPOOLDIR_SVR4 */
  54.  
  55. /* Get the status of a system.  This assumes that we are in the spool
  56.    directory.  */
  57.  
  58. boolean
  59. fsysdep_get_status (qsys, qret, pfnone)
  60.      const struct uuconf_system *qsys;
  61.      struct sstatus *qret;
  62.      boolean *pfnone;
  63. {
  64.   char *zname;
  65.   FILE *e;
  66.   char *zline;
  67.   char *zend, *znext;
  68.   boolean fbad;
  69.   int istat;
  70.  
  71.   if (pfnone != NULL)
  72.     *pfnone = FALSE;
  73.  
  74.   zname = zsysdep_in_dir (".Status", qsys->uuconf_zname);
  75.   e = fopen (zname, "r");
  76.   if (e == NULL)
  77.     {
  78.       if (errno != ENOENT)
  79.     {
  80.       ulog (LOG_ERROR, "fopen (%s): %s", zname, strerror (errno));
  81.       ubuffree (zname);
  82.       return FALSE;
  83.     }
  84.       zline = NULL;
  85.     }
  86.   else
  87.     {
  88.       size_t cline;
  89.  
  90.       zline = NULL;
  91.       cline = 0;
  92.       if (getline (&zline, &cline, e) <= 0)
  93.     {
  94.       xfree ((pointer) zline);
  95.       zline = NULL;
  96.     }
  97.       (void) fclose (e);
  98.     }
  99.  
  100.   if (zline == NULL)
  101.     {
  102.       /* There is either no status file for this system, or it's been
  103.      truncated, so fake a good status.  */
  104.       qret->ttype = STATUS_COMPLETE;
  105.       qret->cretries = 0;
  106.       qret->ilast = 0;
  107.       qret->cwait = 0;
  108.       qret->zstring = NULL;
  109.       if (pfnone != NULL)
  110.     *pfnone = TRUE;
  111.       ubuffree (zname);
  112.       return TRUE;
  113.     }
  114.  
  115.   /* It turns out that scanf is not used much in this program, so for
  116.      the benefit of small computers we avoid linking it in.  This is
  117.      basically
  118.  
  119.      sscanf (zline, "%d %d %ld %d", &qret->ttype, &qret->cretries,
  120.              &qret->ilast, &qret->cwait);
  121.  
  122.      except that it's done with strtol.  */
  123.  
  124.   fbad = FALSE;
  125.   istat = (int) strtol (zline, &zend, 10);
  126.   if (zend == zline)
  127.     fbad = TRUE;
  128.  
  129. #if MAP_STATUS
  130.   /* On some systems it may be appropriate to map system dependent status
  131.      values on to our status values.  */
  132.   {
  133.     int i;
  134.  
  135.     for (i = 0; i < CMAPENTRIES; ++i)
  136.       {
  137.     if (aiMapstatus[i] == istat)
  138.       {
  139.         istat = i;
  140.         break;
  141.       }
  142.       }
  143.   }
  144. #endif /* MAP_STATUS */
  145.  
  146.   if (istat < 0 || istat >= (int) STATUS_VALUES)
  147.     istat = (int) STATUS_COMPLETE;
  148.   qret->ttype = (enum tstatus_type) istat;
  149.   znext = zend;
  150.   qret->cretries = (int) strtol (znext, &zend, 10);
  151.   if (zend == znext)
  152.     fbad = TRUE;
  153.   znext = zend;
  154.   qret->ilast = strtol (znext, &zend, 10);
  155.   if (zend == znext)
  156.     fbad = TRUE;
  157.   znext = zend;
  158.   qret->cwait = (int) strtol (znext, &zend, 10);
  159.   if (zend == znext)
  160.     fbad = TRUE;
  161.  
  162.   if (! fbad)
  163.     {
  164.       znext = zend;
  165.       while (isspace (BUCHAR (*znext)))
  166.     ++znext;
  167.       if (*znext == '\0')
  168.     qret->zstring = NULL;
  169.       else
  170.     {
  171.       if (*znext == '"')
  172.         ++znext;
  173.       qret->zstring = zbufcpy (znext);
  174.       zend = qret->zstring + strlen (qret->zstring);
  175.       while (zend != qret->zstring && *zend != ' ')
  176.         --zend;
  177.       if (*zend == '"' && zend != qret->zstring)
  178.         --zend;
  179.       if (zend != qret->zstring)
  180.         *zend = '\0';
  181.       else
  182.         {
  183.           ubuffree (qret->zstring);
  184.           qret->zstring = NULL;
  185.         }
  186.     }
  187.     }
  188.  
  189.   xfree ((pointer) zline);
  190.  
  191.   if (fbad)
  192.     {
  193.       ulog (LOG_ERROR, "%s: Bad status file format", zname);
  194.       ubuffree (zname);
  195.       return FALSE;
  196.     }
  197.  
  198.   ubuffree (zname);
  199.  
  200.   return TRUE;
  201. }
  202.  
  203. /* Set the status of a remote system.  This assumes the system is
  204.    locked when this is called, and that the program is in the spool
  205.    directory.  */
  206.  
  207. boolean
  208. fsysdep_set_status (qsys, qset)
  209.      const struct uuconf_system *qsys;
  210.      const struct sstatus *qset;
  211. {
  212.   char *zname;
  213.   FILE *e;
  214.   int istat;
  215.  
  216.   zname = zsysdep_in_dir (".Status", qsys->uuconf_zname);
  217.  
  218.   e = esysdep_fopen (zname, TRUE, FALSE, TRUE);
  219.   ubuffree (zname);
  220.   if (e == NULL)
  221.     return FALSE;
  222.   istat = (int) qset->ttype;
  223.  
  224. #if MAP_STATUS
  225.   /* On some systems it may be appropriate to map istat onto a system
  226.      dependent number.  */
  227.   if (istat >= 0 && istat < CMAPENTRIES)
  228.     istat = aiMapstatus[istat];
  229. #endif /* MAP_STATUS */
  230.  
  231.   fprintf (e, "%d %d %ld %d ", istat, qset->cretries, qset->ilast,
  232.        qset->cwait);
  233.  
  234. #if SPOOLDIR_SVR4
  235.   fprintf (e, "\"%s\"", azStatus[(int) qset->ttype]);
  236. #else
  237.   fprintf (e, "%s", azStatus[(int) qset->ttype]);
  238. #endif
  239.  
  240.   fprintf (e, " %s\n", qsys->uuconf_zname);
  241.   if (fclose (e) != 0)
  242.     {
  243.       ulog (LOG_ERROR, "fclose: %s", strerror (errno));
  244.       return FALSE;
  245.     }
  246.  
  247.   return TRUE;
  248. }
  249.